package com.itheima.health.service.impl;

import com.itheima.health.dao.OrdersettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    /**
     * 传递的参数
     * date（格式：2019-8）
     * 构建的数据List<Map>
     * map.put("date",1);
     * map.put("number",120);
     * map.put("reservations",10);
     * <p>
     * 查询方案：SELECT * FROM t_ordersetting WHERE orderDate LIKE '2019-08-%'
     * 查询方案：SELECT * FROM t_ordersetting WHERE orderDate BETWEEN '2019-9-1' AND '2019-9-31'
     */
//根据日期查询预约设置数据
    @Transactional
    @Override
    public List<Map> getOrderSettingByMonth(String date) {//2019-3
        // 1.组织查询Map，dateBegin表示月份开始时间，dateEnd月份结束时间
        String dateBegin = date + "-1";//2019-3-1
        String dateEnd = date + "-31";//2019-3-31
        Map map = new HashMap();
        map.put("dateBegin", dateBegin);
        map.put("dateEnd", dateEnd);
        List<OrderSetting> list = ordersettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number", orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations", orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }


    @Transactional
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        log.info("count", count);
        if (count > 0) {
            //当前日期已经进行了预约设置，需要进行修改操作
            ordersettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //当前日期没有进行预约设置，进行添加操作
            ordersettingDao.add(orderSetting);
        }
    }

    //批量添加

    @Transactional
    @Override
    public void excelFileadd(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //检查此数据（日期）是否存在
                long count = ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //已经存在，执行更新操作
                    ordersettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //不存在，执行添加操作
                    ordersettingDao.add(orderSetting);
                }
            }
        }
    }

}